package azcat.planner.workers;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import azcat.planner.schedule.Schedule;
import azcat.planner.tools.Tool;

public class NeededWorkforceEstimator {
	private ArrayList<Tool> neededTools;
	private ArrayList<Worker> workforce = new ArrayList<Worker>();
	private ArrayList<Worker> neededworkforce = new ArrayList<Worker>();
	
	public NeededWorkforceEstimator(ArrayList<Tool> neededTools){
		this.neededTools = neededTools;
		accessWorkersSchedule();
	}
	
	public ArrayList<Worker> assignWorkers(){
		for(int i=0; i< neededTools.size(); i++){
			estimateNeededWorkers(neededTools.get(i).getSchedule(), neededTools.get(i));
		}
		//System.out.println("size: "+neededworkforce.size());
		return this.neededworkforce;
	}
	
	private void estimateNeededWorkers(ArrayList<Schedule> s, Tool t){
		int days = t.getMaxWorkers();
		int count =0;
		int index = 0;
		int dayname;
		//ArrayList<Integer> unique = new ArrayList<Integer>();
		HashMap<Integer, ArrayList<Schedule>> unique = new HashMap<Integer, ArrayList<Schedule>>();
		for(int i=1; i<=7 ; i++){
			ArrayList<Schedule> sch = new ArrayList<Schedule>();
			for(int j =0; j< s.size(); j++){
				dayname = s.get(j).getCalendar().get(Calendar.DAY_OF_WEEK);
				if(dayname == i){
					sch.add(s.get(j));
				}
			}
			unique.put(i, sch);			
		}
		
		for(int i=1; i<=7 ; i++){
			index =0; count =1;
			while(count <= days){				
				//System.out.println("i= " +i);
				if(workforce.get(index).getAvailable(i+"").equals("1")){//if worker is available on day of the week					
					if(neededworkforce.contains(workforce.get(index))){//if is on the list already update the schedule
						neededworkforce.remove(neededworkforce.indexOf(workforce.get(index)));//remove it so we have the exact same object
						
						workforce.get(index).setSchedule(unique.get(i), t.getName());//update the schedule
						//workforce.get(index).setWorkingOnTool(t.getName());
						neededworkforce.add(workforce.get(index));//add it to the list
						count++;
					}
					else{					
						workforce.get(index).setSchedule(unique.get(i), t.getName());//then set the schedule for that worker to that of the tool in that day
						//workforce.get(index).setWorkingOnTool(t.getName());
						neededworkforce.add(workforce.get(index));//add it to the list
						count++;
					}
				}
				index++;
			}
		}		
	}	
	/**
	 * read the workers schedule
	 */
	private void accessWorkersSchedule(){
		String[] info, days;
		BufferedReader inputStream = null;		
		try {
            inputStream = new BufferedReader(new FileReader("workforce.csv"));
            String l;
            while ((l = inputStream.readLine()) != null) {            	
                info = l.split(",");
                days = info[0].split("-");
                Worker w = new Worker();
                w.setfName(info[1]);
                w.setlName(info[2]);
                for(int i= 0; i< days.length; i++){
                	//System.out.print(b)
                	w.setAvailable(days[i], info[3+i]);
                }
                workforce.add(w);
            }            
		}
		catch(IOException e){	}
		finally {
            if (inputStream != null) {
                try {
					inputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
		}
	}

}
